﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JKF.BusinessLayer.BLL;
using JKF.Substructure;

namespace JKF.Web.Sales.Contract
{
    public partial class Sales_Contract_Add : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MODULE_NAME_EN = "SALES_CONTRACT";
        }
        private static bllPRODUCT_SORT productSort = new bllPRODUCT_SORT();
        private static bllSALES_CONTRACT salesContractMgr = new bllSALES_CONTRACT();
        private static bllCUSTOMER customerMgr = new bllCUSTOMER();
        private static bllPRODUCT productMgr = new bllPRODUCT();
        private static StringBuilder stringBuilder = new StringBuilder();

        public string ProductSort
        {
            get
            {
                stringBuilder.Clear();
                var psorts = productSort.GetListArray(" [type]='1'");
                foreach (var sorTinfo in psorts)
                {
                    stringBuilder.AppendFormat("<option value=\"{0}\">{1}</option>", Server.HtmlEncode(sorTinfo.SORT_KEY.ToString()), Server.HtmlEncode(sorTinfo.NAME));
                }
                return stringBuilder.ToString();
            }
        }

        /// <summary>
        /// 合同编号
        /// </summary>
        private string SalesContractKey
        {
            get { return P("contractKey"); }
        }

        /// <summary>
        /// 当前待编辑对象
        /// </summary>
        public string SalesContract
        {
            get
            {
                var contractKey = SalesContractKey;
                if (contractKey.IsGuidB())
                {
                    var contract = salesContractMgr.GetModel(contractKey.ToGuid());
                    if (contract == null) return "null";
                    var product = productMgr.GetModel(contract.PRODUCT_KEY);
                    var customer = customerMgr.GetModel(contract.CUSTOMER_KEY);
                    return new
                    {
                        ContractKey = contract.CONTRACT_KEY.ToString(),
                        CustomerKey = customer.CUSTOMER_KEY.ToString(),
                        CustomerName = customer.NAME,
                        CardNo = customer.CARD_NO,
                        Mobile = customer.MOBILE,
                        ProductSort = product.SORT_KEY,
                        ProductKey = product.PRODUCT_KEY.ToString(),
                        ProductMode = product.MODEL,
                        Price = contract.PRICE.ToString(),
                        BarCode = contract.BAR_CODE,
                        ContractCode = contract.CONTRACT_CODE,
                        DatePurchase = contract.DATE_PURCHASE.HasValue ? contract.DATE_PURCHASE.Value.ToString("yyyy-MM-dd") : "",
                        Money = contract.MONEY.HasValue ? contract.MONEY.Value.ToString() : "",
                        AddPoint = contract.ADD_POINT.HasValue ? contract.ADD_POINT.ToString() : "",
                        RepairTerm = contract.REPAIR_TERM,
                        ClosingDate = contract.CLOSING_DATE.HasValue ? contract.CLOSING_DATE.Value.ToString("yyyy-MM-dd") : "",
                        Memo = contract.MEMO,
                        DateProduced = contract.DATE_PRODUCED.ToString("yyyy-MM-dd")
                    }.ToJson();
                }
                return "null";
            }
        }

        public string PointRate
        {
            get
            {
                var paramVl = bllSYS_PARAM.GetPointRate();
                return string.IsNullOrEmpty(paramVl) ? "1" : paramVl;
            }
        }
    }
}